﻿using System;
using SaleManagement.Web.App_Code;
using SaleManagement.Services;
using SaleManagement.Domains;

namespace SaleManagement.Web.Pages
{
  public partial class GuaranteeList : BasePage
  {
    #region Properties
    private const string SEARCH_CUSTOMER_CODE = "GuaranteeList_Search_Customer_Code";
    private const string SEARCH_CUSTOMER_NAME = "GuaranteeList_Search_Customer_Name";
    private const string SEARCH_CUSTOMER_PHONE = "GuaranteeList_Search_Phone";
    private const string SEARCH_FROMDATE = "GuaranteeList_Search_FromDate";
    private const string SEARCH_TODATE = "GuaranteeList_Search_ToDate";

    private string m_innerError = string.Empty;

    #endregion

    #region Events
    protected void Page_Load(object sender, EventArgs e)
    {
      if (!IsPostBack && !IsCallback)
      {
        deFromDate.Date = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 1);
        deToDate.Date = DateTime.Now;

        #region Load Session

        txtCustomerCode.Text = GetSessionSearch(SEARCH_CUSTOMER_CODE);
        txtCustomerName.Text = GetSessionSearch(SEARCH_CUSTOMER_NAME);
        txtCustomerPhone.Text = GetSessionSearch(SEARCH_CUSTOMER_PHONE);
        if (GetSessionSearch(SEARCH_FROMDATE) != null)
        {
          if (GetSessionSearch(SEARCH_FROMDATE) != string.Empty)
          {
            deFromDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_FROMDATE));
          }
          else
          {
            deFromDate.Value = null;
          }
        }

        if (GetSessionSearch(SEARCH_TODATE) != null)//not in session
        {
          if (GetSessionSearch(SEARCH_TODATE) != string.Empty)//empty value
          {
            deToDate.Date = Convert.ToDateTime(GetSessionSearch(SEARCH_TODATE));
          }
          else
          {
            deToDate.Value = null;
          }
        }

        #endregion


        LoadList();
      }
    }

    protected void gvGuarantee_CustomCallback(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewCustomCallbackEventArgs e)
    {
      string[] para = e.Parameters.Split('|');
      if (para[0] == "Delete")
      {
        m_innerError = DeleteGuarantee(Convert.ToInt32(para[1]));
      }

      LoadList();
    }

    protected void gvGuarantee_CustomJSProperties(object sender, DevExpress.Web.ASPxGridView.ASPxGridViewClientJSPropertiesEventArgs e)
    {
      e.Properties["cpInnerError"] = m_innerError;
    }

    #endregion

    #region Helpers

    private void LoadList()
    {
      string customerCode = Utilities.SafeString(txtCustomerCode.Text);
      string customerName = Utilities.SafeString(txtCustomerName.Text);
      string customerPhoneNumber = Utilities.SafeString(txtCustomerPhone.Text);
      DateTime? fromDate = deFromDate.Value as DateTime?;
      DateTime? toDate = deToDate.Value as DateTime?;

      ConfigSessionSearch(SEARCH_CUSTOMER_CODE, customerCode);
      ConfigSessionSearch(SEARCH_CUSTOMER_NAME, customerName);
      ConfigSessionSearch(SEARCH_CUSTOMER_PHONE, customerPhoneNumber);
      ConfigSessionSearch(SEARCH_FROMDATE, fromDate.HasValue ? fromDate.Value.ToString() : string.Empty);
      ConfigSessionSearch(SEARCH_TODATE, toDate.HasValue ? toDate.Value.ToString() : string.Empty);
      if (toDate.HasValue && toDate.Value != DateTime.MinValue)
      {
        toDate = toDate.Value.AddHours(23).AddMinutes(59);
      }
      gvGuarantee.DataSource = new GuaranteeListService().Search(customerCode, customerName, customerPhoneNumber, fromDate, toDate);
      gvGuarantee.DataBind();
    }

    private string DeleteGuarantee(int id)
    {
      #region Check detail
      TList<Guarantee> detailList = new GuaranteeService().GetByGuanranteeListId(id);
      if (detailList.Count > 0)
      {
        return "Không thể xóa hóa đơn vì các thông tin chi tiết vẫn còn lưu.";
      }
      #endregion

      #region Delete DB
      try
      {
        new GuaranteeListService().Delete(id);
        return string.Empty;
      }
      #endregion
      catch (Exception ex)
      {
        WriteLog("GuaranteeList.aspx::Delete", ex);
        return "Có lỗi trong quá trình lưu dữ liệu";
      }
    }

    #endregion
  }
}
